package com.bc.bczbweb.controller;

import com.bc.bccore.zbconfig.ZbConfig;
import com.bc.bcmodel.consts.GlobalSettings;
import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Name ManageUser
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/8/14 12:25
 * @Version 1.0
 **/
@Controller
@RequestMapping("")
public class ManageUserController {
    @RequestMapping("")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        String cookieValue = CookieUtils.getCookie(request, response, GlobalSettings.MANAGE_COOKIENAME);
        if (!StringUtils.isEmpty(cookieValue)) {
            try {
                response.sendRedirect(ZbConfig.getSitePath() + "/main/index");
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ModelAndView view = new ModelAndView("manage/login");
        view.addObject("staticPath", ZbConfig.getSitePath());
        return view;
    }

    @RequestMapping("loginout")
    public void loginout(HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.deleteCookie(request, response, GlobalSettings.MANAGE_COOKIENAME);
        try {
            response.sendRedirect(ZbConfig.getSitePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
